package com.wkhyapp.lm.fragment;


import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tbruyelle.rxpermissions.RxPermissions;
import com.wkhyapp.lm.MainActivity;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.base.SuperFragment;
import com.wkhyapp.lm.contract.MyPresenter;
import com.wkhyapp.lm.contract.MyView;
import com.wkhyapp.lm.http.net.AddGoodsRequest;
import com.wkhyapp.lm.http.vo.Member;
import com.wkhyapp.lm.utils.ImageLoadUtil;
import com.wkhyapp.lm.utils.ImgDonwloads;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.view.CheckInActivity;
import com.wkhyapp.lm.view.CollectionActivity;
import com.wkhyapp.lm.view.FeedbackActivity;
import com.wkhyapp.lm.view.LoginActivity;
import com.wkhyapp.lm.view.MarketActivity;
import com.wkhyapp.lm.view.NoticeActivity;
import com.wkhyapp.lm.view.RuheDaliActivity;
import com.wkhyapp.lm.view.SettingActivity;
import com.wkhyapp.lm.view.ShareAppActivity;
import com.wkhyapp.lm.view.UpImageActivity;
import com.wkhyapp.lm.view.UserInfoActivity;
import com.wkhyapp.lm.view.XiangCeActivity;
import com.wkhyapp.lm.weigit.dialog.ContactAppDialog;

import butterknife.BindView;
import rx.functions.Action1;

public class MyFragment  extends SuperFragment<MyPresenter> implements MyView {


    @BindView(R.id.gfxx_rl)
    RelativeLayout gfxx_rl;
    @BindView(R.id.wdxc_rl)
    RelativeLayout wdxc_rl;
    @BindView(R.id.sett_rl)
    RelativeLayout sett_rl;
    @BindView(R.id.sc_rl)
    RelativeLayout sc_rl;
    @BindView(R.id.wt_rl)
    RelativeLayout wt_rl;
    @BindView(R.id.head_iv)
    ImageView head_iv;
    @BindView(R.id.name_tv)
    TextView name_tv;
    @BindView(R.id.id_tv)
    TextView id_tv;
    @BindView(R.id.copy_but)
    TextView copy_but;
    @BindView(R.id.share_rl)
    RelativeLayout share_rl;
    @BindView(R.id.lx_rk)
    RelativeLayout lx_rk;
    @BindView(R.id.sqgh_rl)
    RelativeLayout sqgh_rl;
    private Member member;
    @BindView(R.id.rm_rl)
    RelativeLayout rm_rl;
    @BindView(R.id.rhdl_rl)
    RelativeLayout rhdl_rl;
    @BindView(R.id.sqgh_iv)
    ImageView sqgh_iv;

    @Override
    public void initUI() {
        ImageLoadUtil.setImage_Normal(context,R.drawable.sqgh,sqgh_iv);
    }

    @Override
    protected void initData() {
        showLoad("加载中...");
        mPresenter.getData(MemberUtils.getUid());
    }

    @Override
    protected void initListener() {
        head_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(UserInfoActivity.class);
            }
        });

        gfxx_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            goTo(NoticeActivity.class);

            }
        });

        wdxc_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(member.getStatus()>=3){
                    goTo(XiangCeActivity.class);
                }else{
                    TToast("您还不是商家,请联系客服开通");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            final  ContactAppDialog dialog = new ContactAppDialog(context, "","","");
                            dialog.init();
                            dialog.show();

                            dialog.setCallBack(new ContactAppDialog.callBack() {
                                @Override
                                public void downLoad(String url) {
                                    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M &&
                                            MainActivity.mainActivity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
                                        requestPermissions(new String[]{
                                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                                Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
                                    }else{
                                        ImgDonwloads.donwloadImg(context, "http://app-img.wkhyw.com/ewm.jpg");
                                    }

                                }

                                @Override
                                public void copy(String str) {
                                    dialog.close();
                                    TToast("复制成功");
                                    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                                    ClipData clipData = ClipData.newPlainText(null, str);
                                    // 把数据集设置（复制）到剪贴板
                                    clipboard.setPrimaryClip(clipData);
                                    Intent lan = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                                    Intent intent = new Intent(Intent.ACTION_MAIN);
                                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    intent.setComponent(lan.getComponent());
                                    startActivity(intent);

                                }
                            });
                        }
                    },1500);
                }
            }
        });

        sett_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goTo(SettingActivity.class);
            }
        });

        sc_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(CollectionActivity.class);
            }
        });

        share_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(ShareAppActivity.class);
            }
        });

        wt_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(FeedbackActivity.class);
            }
        });
        rm_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(MarketActivity.class);
            }
        });

        rhdl_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(RuheDaliActivity.class);
            }
        });

        copy_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TToast("复制成功");
                ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText(null, member.getId()+"");
                // 把数据集设置（复制）到剪贴板
                clipboard.setPrimaryClip(clipData);
            }
        });

        lx_rk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               final  ContactAppDialog dialog = new ContactAppDialog(context, "","","");
                dialog.init();
                dialog.show();
                dialog.setCallBack(new ContactAppDialog.callBack() {
                    @Override
                    public void downLoad(String url) {

                        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M &&
                                MainActivity.mainActivity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
                            requestPermissions(new String[]{
                                    Manifest.permission.READ_EXTERNAL_STORAGE,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
                        }else{
                            ImgDonwloads.donwloadImg(context, "http://app-img.wkhyw.com/ewm.jpg");
                        }


                    }

                    @Override
                    public void copy(String str) {
                        dialog.close();
                        TToast("复制成功");
                        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                        ClipData clipData = ClipData.newPlainText(null, str);
                        // 把数据集设置（复制）到剪贴板
                        clipboard.setPrimaryClip(clipData);
                        Intent lan = context.getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_LAUNCHER);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setComponent(lan.getComponent());
                        startActivity(intent);

                    }
                });
            }
        });

        sqgh_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(MemberUtils.isLogin()){
                    goTo(CheckInActivity.class);
                }else{
                    TToast("请先登录");
                    goTo(LoginActivity.class);
                }
            }
        });
    }

    @Override
    protected MyPresenter createPresenter() {
        return mPresenter = new MyPresenter(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    public void setMember(Member member) {
        this.member = member;
        dismissLoad();
        ImageLoadUtil.setCirlcleImage_Normals(context,member.getHeadImg(),head_iv);
        name_tv.setText(member.getNickname());
        id_tv.setText("账号:"+member.getId());
        MemberUtils.saveMember(member);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getData(MemberUtils.getUid());
        if(MemberUtils.isLogin()){
            mPresenter.getData(MemberUtils.getUid());
        }else{
            goTo(LoginActivity.class);
        }
    }
}
